package com.example.sell.service.impl;

import com.example.sell.bean.ProductInfo;
import com.example.sell.common.ResultVOUtil;
import com.example.sell.enums.ProductStatus;
import com.example.sell.enums.ResultVO;
import com.example.sell.repository.ProductRepository;
import com.example.sell.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;

import static com.example.sell.enums.ProductStatus.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author ouyangzhixiang
 * @date 2018/6/28
 */
@Service
public class ProductServiceImpl implements ProductService {
    private final ProductRepository productRepository;

    @Autowired
    public ProductServiceImpl(ProductRepository productRepository) {
        this.productRepository = productRepository;
    }

    /**
     * 查询当个商品信息
     *
     * @param productId 商品Id
     * @return
     */
    @Override
    public ResultVOUtil <Optional<ProductInfo>> findOne(String productId) {
        Optional<ProductInfo> productInfo = productRepository.findById(productId);
        if (productInfo.isPresent()) {
            // 查询该对象不是空返回提示信息TODO
            return ResultVOUtil.createBySuccess(productInfo);
        }
        //该对象是空的情况下
        return ResultVOUtil.createByErroCodeMessage(ResultVO.ERROR.getCode(),ResultVO.ERROR.getDesc());
    }

    /**
     * 查询所有商品信息
     *
     * @return
     */
    @Override
    public ResultVOUtil<List<ProductInfo>> findAll() {
        List<ProductInfo> result = productRepository.findAll();
        if (result.isEmpty()) {
            return ResultVOUtil.createByErrorMessage("该商品信息不存在");
        }
        return ResultVOUtil.createBySuccessMessage("成功", result);
    }

    /**
     * 添加/更新商品信息
     *
     * @param productInfo
     * @return
     */
    @Override
    public ResultVOUtil<ProductInfo> save(ProductInfo productInfo) {
        ProductInfo addProduct =new ProductInfo();
        if (productInfo.getProductId()!=null){
            addProduct =productRepository.findById(productInfo.getProductId()).get();
        }
        BeanUtils.copyProperties(productInfo,addProduct);
       return ResultVOUtil.createBySuccess(productRepository.save(addProduct));
    }

    /**
     * 查询所有在架商品信息
     *
     * @return
     */
    @Override
    public List<ProductInfo> findUp() {
        return productRepository.findByProductStatusIn(UP.getCode());
    }

    /**
     * 商品上架
     * @param productId
     * @return
     */
    @Override
    public ResultVOUtil<ProductInfo> shelf(String productId,Integer productStatus) {
        ProductInfo productInfo = productRepository.findById(productId).get();
        if (productInfo.getProductStatusEnum()==ProductStatus.UP) {
            return ResultVOUtil.createByErrorMessage("该商品已上架状态");
        }
        productInfo.setProductStatus(productStatus);
        return ResultVOUtil.createBySuccessMessage("商品已上架",productRepository.save(productInfo));
    }

    /**
     * 商品下架
     * @param productId
     * @return
     */
    @Override
    public ResultVOUtil<ProductInfo> dropoff(String productId,Integer productStatus) {
        ProductInfo productInfo = productRepository.findById(productId).get();
        if (productInfo.getProductStatusEnum()==ProductStatus.DOWN) {
            return ResultVOUtil.createByErrorMessage("该商品已下架状态");
        }
        productInfo.setProductStatus(productStatus);
        return ResultVOUtil.createBySuccessMessage("修改商品下架状态成功",productRepository.save(productInfo));
    }


}
